package model.dynamic;

import com.github.crab2died.annotation.ExcelField;
import model.AbstractDepartment;
import model.AbstractModel;

import java.util.ArrayList;
import java.util.List;

abstract public class DyDepartment extends AbstractDepartment
{
	// 等级提升比例
	public static final double[] promoteRates = {0.41, 0.41, 0.25, 0.25, 0.25,
			0.09, 0.09, 0.06, 0.06, 0.04, 0.04};
	// 等级最低服役年限
	public static final int[] rankLeastTimes = {2, 2, 2, 2, 2, 3, 3, 5, 5, 4,
			4};
	// 等级最高服役年限
	public static final int[] rankLimitTimes = {5, 5, 7, 7, 7, 8, 8, 12, 12, 10,
			10};
	// 等级最高服役年龄
	public static final int[] rankLimitAges = {30, 30, 35, 35, 35, 40, 40, 50,
			50, 57, 57};
	// 等级参考最低年龄
	public static final int[] rankLowReferAges = {18, 20, 22, 24, 26, 28, 31,
			34, 39, 44, 48};
	// 等级年限修正系数
	static double rankAmendRatio = 0.1;
	// 年龄限制修正系数
	static double ageAmendRatio = 0.13;

	@ExcelField(title = "动态匹配率", order = 13)
	protected double dynamicMatchRate;

	// 辅助变量
	double dynamicP;

	// 该单位所有完全匹配实有人员链表
	public List<DyModel> matchActualModels = new ArrayList<>();

	public void calculateStatisticData()
	{
		super.calculateStatisticData();
		dynamicMatchRate = dynamicMatchRate();

		if (format != null)
		{
			dynamicMatchRate = Double.parseDouble(format.format(dynamicMatchRate));
		}
	}

	public void addSubDepartment(AbstractDepartment subDepartment)
	{
		super.addSubDepartment(subDepartment);
		if (!hasSubDepartment)
			return;
		matchActualModels
				.addAll(((DyDepartment) subDepartment).matchActualModels);
	}

	protected void transportTotalMatchModel(AbstractModel model)
	{
		matchActualModels.add((DyModel) model);
	}

	protected double dynamicMatchRate()
	{
		dynamicP = 0;
		if (hasSubDepartment())
		{
			for (AbstractDepartment subDepartment : subDepartments)
				dynamicP += ((DyDepartment) subDepartment).dynamicP;
			return dynamicP / getAuthorizedNumber();
		}

		for (DyModel actualModel : matchActualModels)
		{
			dynamicP += dynamicRate(actualModel) * actualModel.dyNumber;
		}
		return dynamicP / getAuthorizedNumber();
	}

	protected double dynamicRate(DyModel dModel)
	{
		int index = dModel.getRank() - 1;
		int rankLimitTime = rankLimitTimes[index];// 等级最高年限
		int rankServiceTime = dModel.rankServiceTime;// 等级服役时间
		int serviceAgeDiff = rankLimitAges[index] - dModel.serviceAge;// 与最高服役年龄差
		int diff = rankLimitTime - rankServiceTime;
		double promote = 1 - promoteRates[index];
		if (diff < serviceAgeDiff)
			return 1 - Math.pow(promote, diff + 1) * rankAmendRatio;
		return 1 - Math.pow(promote, serviceAgeDiff) * ageAmendRatio;
	}

	public double getDynamicMatchRate()
	{
		return dynamicMatchRate;
	}
	public void setDynamicMatchRate(double dynamicMatchRate)
	{
		this.dynamicMatchRate = dynamicMatchRate;
	}
}
